<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH.'constants.inc');
	require_once('page.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
	require_once('utils.inc');			
	require_once('utilsPlati.inc');		
	
	session_start();
	check_permissions(CT_PERM_PLATI);
		
	html_header("LANG_vizualizare_incasari", null, "view_incasare");
	
	select_js_language();
	
	$perPage = 10;
?>	
<?php
	for ($i = 0; $i < count($currencies); $i++)
		$monedaValues[] = $i;
		
	//get a db connection handle
	$db = db_connect();
	
	try {
		if (isset($_GET['numar_comanda']))
			$nr_comanda = $_GET['numar_comanda'];
		else 
			$nr_comanda = "";
		$plati = getLastNPlati($db, 0, $perPage, 0, "data", 0, $_SESSION['client_logat']->id_client, null, null, $nr_comanda, null);		
	}
	catch (QueryGetLast20PlatiException $e) {
		redirectOnError(CT_VIEW_INCASARE_GET_N_PLATI);
	}
	
	$linkuri = array(LANG_Home => "home.php", LANG_Incasari => "");
    titlu_pagina($linkuri);
    output_error_msg();
    output_notice_msg();
    output_extra_text(LANG_EXTRA_VIEW_INCASARE);
?>
	<script type="text/javascript">
	
		function validateForm() {
			var suma = document.view_plati.suma.value;
			if (suma == "")
				suma = 0;
			return (validateFloat(suma, '<?php echo LANG_SUMA; ?>', true));
		}
	
	</script>
		
    <div align="left" class="container">												
    <table align="left" id="wrapper">
        <form name="view_plati" id="view_plati">    
		    <thead>
			    <tr>
				    <th colspan="8"><?php echo LANG_FILTREAZA; ?></th>
			    </tr>
		    </thead>
		    <tr class="capTabel">			
			    <td><?php echo LANG_Numar_comanda; ?> <input onkeydown="if (event.keyCode == 13) {if (validateForm()) filterPlati('0', '1', '<?php echo $perPage; ?>'); return false} else return true;" type="text" name="nr_comanda" value="<?php if (isset($_GET['numar_comanda'])) echo $_GET['numar_comanda']; ?>"/></td>
			    <td><?php echo LANG_Moneda; ?> 
                    <?php 
                        if (isset($_GET['numar_comanda']))
                            render_combobox("moneda", $monedaValues, $currencies, $plati[0]['moneda']);
                        else 
                            render_combobox("moneda", $monedaValues, $currencies, $_SESSION['user_logat']->moneda);
                    ?>
                </td>
			    <td><?php echo LANG_SUMA; ?> <input onkeydown="if (event.keyCode == 13) {if (validateForm()) filterPlati('0', '1', '<?php echo $perPage; ?>'); return false} else return true;" type="text" name="suma" maxlenth="6" size="5" /></td>
			    <td><?php echo LANG_DATA; ?></td>
			    <td><?php echo render_date_field_button('', 'data', 'main');?>
                <td><input type="button" name="filter" value="<?php echo LANG_Search; ?>" onclick="javascript: if (validateForm()) filterPlati('0', '1', '<?php echo $perPage; ?>');"/></td>
			    <td><input type="button" name="filter" value="<?php echo LANG_DELETE_FILTER; ?>" onclick="javascript: deleteFilter('0', '1', '<?php echo $perPage; ?>');"/></td>
		    </tr>
        </form>    
	</table>
    
	</div>
	
<?php
	echo '<div id="platiTable" class="container">';
		global $totals;
        if (isset($_GET['numar_comanda']))
		    renderPlatiTable(0, $plati, $perPage, 1, 1, $_GET['numar_comanda']);
        else 
            renderPlatiTable(0, $plati, $perPage, 0, 1, $_GET['numar_comanda']);
	echo '</div>';

	//cache the first page
	?>
		<script type="text/javascript">
			cacheArray[1] = document.getElementById("platiTable").innerHTML;
		</script>
	<?php
	//end cache
?>

<?php
	html_footer();
?>